package com.pt.pwmr;

import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaRouter;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextView tv_show;
    // 定义一个GLSurfaceView类，用于显示3D视图
    private GLSurfaceView surface_view;
    private MediaRouter mMediaRouter;
    private DemoPresentation mPresentation;
    //应用是否是可见状态的标志位
    private boolean mPaused;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * 获取到媒体路由，当媒体路由被选择或取消选择或者路由首选的presentation显示屏幕发生变化时，
         * 它都会发送通知消息。一个应用程序可以非常简单通过地观察这些通知消息来自动地在首选的presentation
         * 显示屏幕上显示或隐藏一个presentation。
         */
        mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);

        tv_show = (TextView) findViewById(R.id.tv_show);
        surface_view = (GLSurfaceView) findViewById(R.id.surface_view);
        // 设置我们要渲染的图形为CubeRenderer，一个矩形3D模型
        surface_view.setRenderer(new CubeRenderer());

    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置对媒体路由变化的监听
        mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mRouteCallBack);
        updatePresentation();
        mPaused=false;
        updatePresentation();
    }

    private MediaRouter.SimpleCallback mRouteCallBack=new MediaRouter.SimpleCallback(){

        @Override
        public void onRouteSelected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
            super.onRouteSelected(router, type, info);
            updatePresentation();
        }

        @Override
        public void onRouteUnselected(MediaRouter router, int type,
                                      MediaRouter.RouteInfo info) {
            super.onRouteUnselected(router, type, info);
            updatePresentation();
        }

        @Override
        public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) {
            super.onRouteChanged(router, info);
            updatePresentation();
        }

    };



    @Override
    protected void onPause() {
        super.onPause();
        mPaused=true;
        //取消媒体路由的监听
        mMediaRouter.removeCallback(mRouteCallBack);
        //更新显示内容
        updateContents();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mPresentation!=null){
            mPresentation.dismiss();
            mPresentation=null;
        }
    }

    private void updatePresentation() {
        //获取到被选中的媒体路由，类型为视频路由
        MediaRouter.RouteInfo route = mMediaRouter
                .getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
        //获取到路由推荐的presentation display
        Display presentationDisplay=route!=null ? route.getPresentationDisplay():null;
        if(presentationDisplay==null){
            Log.i(TAG,"没有找到视频路由推荐的display");
        }else{
            Log.i(TAG,presentationDisplay.getName());
        }
        //如果presentation已经存在且它的显示屏不是媒体路由推荐的显示屏，则取消presentation
        if(mPresentation!=null && mPresentation.getDisplay()!=presentationDisplay){
            Log.i(TAG, "Dismissing presentation because the current route no longer "
                    + "has a presentation display.");
            mPresentation.dismiss();
            mPresentation=null;
        }
        //显示presentation
        if(mPresentation==null && presentationDisplay!=null){
            mPresentation=new DemoPresentation(this, presentationDisplay);
            //设置presentation的解除监听
            mPresentation.setOnDismissListener(mPresentationDismissListener);
            mPresentation.show();
        }
        updateContents();
    }

    /**
     * 更新presentation中显示的内容，如果presentation已经创建并显示则将内容显示在presentation上
     * 如果没有创建presentation则将内容显示在主屏幕上
     */
    private void updateContents() {
        //显示在次级屏蔽上
        if(mPresentation!=null){
            tv_show.setText("在屏幕："+mPresentation.getDisplay().getName()+"中显示presentation的内容。");
            //隐藏主屏幕的surfaceView
            surface_view.setVisibility(View.INVISIBLE);
            surface_view.onPause();
            //如果当前应用处于不可见状态
            if(mPaused){
                mPresentation.getSurface_view().onPause();
            }else{
                mPresentation.getSurface_view().onResume();
            }
        }else {
            //将内容显示到本机屏幕上
            tv_show.setText("在本机屏幕："+getWindowManager().getDefaultDisplay().getName()+"中显示内容。");
            surface_view.setVisibility(View.VISIBLE);
            if(mPaused){
                surface_view.onPause();
            }else{
                surface_view.onResume();
            }
        }
    }

    private DialogInterface.OnDismissListener mPresentationDismissListener=new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if(dialog==mPresentation){
                mPresentation=null;
                updateContents();
            }
        }
    };

}